package com.tedu.egweibo.controller;

import com.tedu.egweibo.common.result.JsonResult;
import com.tedu.egweibo.common.result.StatusCode;
import com.tedu.egweibo.mapper.WeiboMapper;
import com.tedu.egweibo.pojo.dto.WeiboDTO;
import com.tedu.egweibo.pojo.entity.User;
import com.tedu.egweibo.pojo.entity.Weibo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.Date;

@Api(tags = "微博模块")
@RestController
@RequestMapping("/v1/weibo/")
@Slf4j
public class WeiboController {
    @Autowired
    private WeiboMapper weiboMapper;

    @ApiOperation("发布微博")
    @PostMapping("insert")
    public JsonResult insert(@RequestBody WeiboDTO weiboDTO, @ApiIgnore HttpSession session) {
        log.debug("weiboDTO:"+weiboDTO);
        User user = (User) session.getAttribute("user");
        if (user == null) {
            return new JsonResult(StatusCode.NOT_LOGIN);
        } else {
            Weibo weibo = new Weibo();
            BeanUtils.copyProperties(weiboDTO, weibo);
            weibo.setUserId(user.getId());
            weibo.setCreated(new Date());
            return JsonResult.success(weiboMapper.insert(weibo));
        }
    }

    @ApiOperation("获取微博首页列表")
    @GetMapping("selectIndex")
    public JsonResult selectIndex() {
        return JsonResult.success(weiboMapper.selectIndex());
    }

    @ApiOperation("获取微博详情页")
    @GetMapping("selectById")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "微博编号", required = true, dataType = "Integer", example = "1"),
            @ApiImplicitParam(name = "username", value = "用户名", required = true, dataType = "String", example = "YuWu")
    })
    public JsonResult selectById(@RequestBody Integer id, @RequestBody String username) {
        return JsonResult.success(weiboMapper.selectById(id, username));
    }
}
